﻿#region Disclaimer/License Info

/* *********************************************** */

// sBlog.Net

// sBlog.Net is a minimalistic blog engine software.

// Homepage: http://sblogproject.net
// Github: http://github.com/karthik25/sBlog.Net

// This project is licensed under the BSD license.  
// See the License.txt file for more information.

/* *********************************************** */

#endregion
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using sBlog.Net.Common;
using sBlog.Net.DependencyManagement;
using sBlog.Net.Domain.Entities;
using sBlog.Net.Domain.Interfaces;

namespace sBlog.Net.Controllers
{
    public class RecentsController : Controller
    {
        private readonly IPost _postRepository;
        private readonly ITagMapping _tagMappingReponsitory;
        private readonly ITag _tagReponsitory;

        private static readonly Random getrandom = new Random();
        private static readonly object syncLock = new object();

        public RecentsController()
        {
            _tagReponsitory = InstanceFactory.CreateTagInstance();
            _tagMappingReponsitory = InstanceFactory.CreateTagMappingInstance();
            _postRepository = InstanceFactory.CreatePostInstance();
        }

        public static int GetRandomNumber(int min, int max)
        {
            lock (syncLock)
            { // synchronize
                return getrandom.Next(min, max);
            }
        }

        [ChildActionOnly]
        public ActionResult NewPosts()
        {
            var model = MemcaheService.GetNewPosts();
            return PartialView("NewPost", model);
        }

        [ChildActionOnly]
        public ActionResult MostViewPosts(string categorySlug)
        {
            var postEntities = MemcaheService.GetMostViewPostByCatSlug(categorySlug);
            return PartialView("MostViewPosts", postEntities);
        }

        [ChildActionOnly]
        public ActionResult MostTagPosts()
        {
            List<TagEntity> model = new List<TagEntity>();
            model = MemcaheService.MostTagPosts();
            return PartialView("MostTagPosts", model);
        }

        protected override void Dispose(bool disposing)
        {
            _tagReponsitory.Dispose();
            _tagMappingReponsitory.Dispose();
            _postRepository.Dispose();
        }
    }
}
